package cn.zxy.test;


import cn.zxy.mapper.DUserMapper;
import cn.zxy.mapper.IuserMapper;
import cn.zxy.mapper.UserMapper;
import cn.zxy.model.User;
import cn.zxy.utils.MyBatisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class MyBatisDB {

    @Test
    public void test() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sessionFactory.openSession();
        List<Object> dbs = session.selectList("dbs");
        System.out.println(dbs);

        //添加操作
        Map<String, Object> map = new HashMap<>();
        map.put("username","赵生蚝");
        map.put("sex","男");
        map.put("address","瓦屋里");
        map.put("birthday","2022-06-01");
        int save = session.insert("save", map);
        System.out.println(save);
        session.commit();

        //查询所有
        List<Map<String,Object>> showUser = session.selectList("showUser");
        System.out.println(showUser);
        is.close();
        session.close();

    }

    @Test
    public void test02() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建SqlSessionFactory 工厂 并拿到获取权
//        SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
//        SqlSessionFactory sessionFactory = sessionFactoryBuilder.build(is);

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

        // 使用SqlSessionFactory 生产sqlsession对象
        SqlSession session = sessionFactory.openSession();
        // 使用session创建mapper对象 代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users){
            System.out.println(user);
        }
        is.close();
        session.close();

    }


    @Test
    public void test03() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建SqlSessionFactory 工厂 并拿到获取权
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用SqlSessionFactory 生产sqlsession对象
        SqlSession session = sessionFactory.openSession();
        //使用session创建mapper对象 代理对象
        IuserMapper mapper = session.getMapper(IuserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users){
            System.out.println(user);
        }
        is.close();
        session.close();

    }
    
    @Test
    public void test04() throws IOException {
        //添加操作
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //开启自动事务
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("侯铁柱");
        user.setSex("女");
        user.setAddress("上海");
        user.setBirthday(new Date());
        mapper.save(user);

        //释放资源
        is.close();
        session.close();
    }

    @Test
    public void test05() throws IOException {
        // 修改操作
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //开启自动事务
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setId(4);
        user.setUsername("生蚝");
        user.setSex("女");
        user.setAddress("上海");
        mapper.update(user);
        //释放资源
        is.close();
        session.close();

    }

    @Test
    public void test06() throws IOException {
        //删除操作
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //开启自动事务
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);

        mapper.delete(6);
        //释放资源
        is.close();
        session.close();

    }

    @Test
    public void test07(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user :all){
            System.out.println(user);
        }
        if (sqlSession!=null){
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Test
    public void test07_2(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        log.debug("user数据库里面的数据是{}",all);
//          log.info("信息数据展示{}",all);
//        log.error("数据报错信息展示{}",all);
        for (User user : all) {
            System.out.println(user);
        }
        if(sqlSession!=null){
            MyBatisUtils.closeSqlSession(sqlSession);
        }

    }

    //注解测试
    @Test
    public void annTest01(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        IuserMapper mapper = sqlSession.getMapper(IuserMapper.class);
        User user = new User();
        user.setUsername("蚝子");
        user.setSex("男");
        user.setAddress("郑州");
        user.setBirthday(new Date());
        mapper.save(user);
        MyBatisUtils.closeSqlSession(sqlSession);
    }

    @Test
    public void annTest02(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        IuserMapper mapper = sqlSession.getMapper(IuserMapper.class);
        User user = new User();
        user.setId(4);
        user.setUsername("荣耀");
        user.setSex("女");
        user.setAddress("漯河");
        mapper.update(user);

        MyBatisUtils.closeSqlSession(sqlSession);
    }

    @Test
    public void SelectKeyTest() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("侯");
        user.setSex("女");
        user.setAddress("北京");
        user.setBirthday(new Date());
        mapper.save(user);

        //打印以下id
        System.out.println("userId=="+user.getId());
        //释放资源
        MyBatisUtils.closeSqlSession(sqlSession);
    }

    //动态sql if测试
    @Test
    public void  DTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        User user=new User();
        user.setId(0);
        user.setUsername("侯");
        List<User> all = mapper.findAll(user);
        log.debug("目前的身份到底是什么{}",all);
        MyBatisUtils.closeSqlSession(sqlSession);
    }

    //修改
    @Test
    public void DupTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("id",8);
         map.put("username","小刚");
        map.put("sex","女");
        int update = mapper.update(map);
        log.debug("修改后的数据是否成功={}",update);
    }

    //插入
    @Test
    public void DinsTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        Map<String,Object> map=new HashMap<>();

        map.put("username","大头儿子");
        map.put("sex","男");
        map.put("address","花园");
        map.put("birthday",new Date());
        int insertMap = mapper.insertMap(map);
        log.debug("添加后的数据是否成功={}",insertMap);
    }

    //模糊查询
    //第一种写法测试
    @Test
    public void SelectLike() throws ParseException {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);

//        List<User> list = mapper.showFindAll("子", null, null,null );
//        List<User> list = mapper.showFindAll(null, "女", null,null );
//        List<User> list = mapper.showFindAll(null, null, "北京",null );

//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date parse = sdf.parse("2023-07-17 00:00:00");

        List<User> list = mapper.showFindAll(null, null, null,"2023-07-17");
        for (User user : list) {
            System.out.println(user);
        }
    }

    //第二种写法测试
    @Test
    public void SelectLike_2(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);

//        List<User> list = mapper.showFindAll("侯", null, null,null );
//        List<User> list = mapper.showFindAll(null, "女", null,null );
//        List<User> list = mapper.showFindAll(null, null, "北京",null );


        List<User> list = mapper.showFindAll(null, null, null,"2023-07-17");
        for (User user : list) {
            System.out.println(user);
        }
    }

    //模糊删除
    //删除多个
    @Test
    public void DeleteTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);

        mapper.deleteAll("生", null, null,null );
//        mapper.deleteAll(null, "女", null,null );
//        mapper.deleteAll(null, null, "北京",null );
//        mapper.deleteAll(null, null, null,"2023-07-17");
    }

    @Test
    public void findByIdsT()  {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        List<Integer> list=new ArrayList<>();
        list.add(23);
        list.add(24);
        list.add(25);
        list.add(26);
        List<User> byIds = mapper.findByIds(list);
        for (User byId : byIds) {
            System.out.println(byId);
        }
        MyBatisUtils.closeSqlSession(sqlSession);
    }

    @Test
    public void inTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);

        long start=System.currentTimeMillis();
        log.info("开始的时间{}",start);

        List<User> userList=new ArrayList<>();
       // userList.stream().forEach(user->mapper.insert(userList));
       User user;
        for (int i = 0; i < 5; i++) {
            user=new User();
            user.setUsername("唐"+i);
            user.setSex("男");
            user.setAddress("河南北京");
            user.setBirthday(new Date());
            userList.add(user);
        }
        mapper.insert(userList);

        long end = System.currentTimeMillis();
        log.info("结束的时间{}",(end-start));

        MyBatisUtils.closeSqlSession(sqlSession);
    }

    @Test
    public void DeleteAllT()  {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        List<Integer> list=new ArrayList<>();
        list.add(23);
        list.add(24);
        list.add(25);
        list.add(26);
        mapper.DeleteAll(list);

        MyBatisUtils.closeSqlSession(sqlSession);
    }

    @Test
    public void pageTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        DUserMapper mapper = sqlSession.getMapper(DUserMapper.class);
        //第一页显示3条数据
        //PageHelper.startPage(1,3);
        //第二页显示3条数据
        PageHelper.startPage(5,5);

        List<User> byAll = mapper.findByAll();
        for (User user : byAll) {
            System.out.println(user);
        }
        PageInfo<User> info=new PageInfo<>(byAll);
        log.info("总条数：{}",info.getTotal());
        log.info("总页数：{}",info.getPages());
        log.debug("当前页：{}",info.getPageNum());
        log.debug("每页显示的数据是：{}",info.getPageSize());
        log.debug("上一页：{}",info.getPrePage());
        log.info("下一页：{}",info.getNextPage());
        log.debug("是否在第一页：{}",info.isIsFirstPage());
        log.debug("是否在最后一页：{}",info.isIsLastPage());

        MyBatisUtils.closeSqlSession(sqlSession);
    }



}
